package refelectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author 12629
 * @Date 2022/4/4 19:21
 * @Description：
 */
public class ReflectClassDemo {

    // 创建对象
    public static void reflectNewInstance() {
        try {
            Class<?> c1 = Class.forName("refelectdemo.Student");

            Student student = (Student)c1.newInstance();

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有的构造方法  屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("refelectdemo.Student");

            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class);

            //私有的本身是封装的,
            constructor.setAccessible(true);

            Student student = (Student)constructor.newInstance("bit",19);

            System.out.println(student);

        } catch (ClassNotFoundException e) {

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射所有的，用私有属性来举例
    public static void reflectPrivateField() {
        try {
            Class<?> c1 = Class.forName("refelectdemo.Student");

            Student student = (Student)c1.newInstance();

            Field field = c1.getDeclaredField("name");

            field.setAccessible(true);
            //student对象的field字段，设置为zhangsan值
            field.set(student,"zhangsan");

            System.out.println(student);

        } catch (ClassNotFoundException e) {

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有方法
    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("refelectdemo.Student");

            Student student = (Student)c1.newInstance();

            //方法名称    方法的参数
            Method method = c1.getDeclaredMethod("function", String.class);

            method.setAccessible(true);

            method.invoke(student,"我是function的参数！");

        } catch (ClassNotFoundException e) {

        }  catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }

}
